// pages/topic/topic.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topic_box: [],
    back_url: '',
    goods_id: 0,
    page: 1,
    searchData: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    getApp().page.onLoad(this, t);
    wx.setNavigationBarTitle({
      title: '选择话题'
    })
    if (t.back_url) {
      this.setData({
        back_url: t.back_url,
        goods_id: t.goods_id,
        topic_id: t.topic_id
      });
    }
    this.getTopicListHot();
  },
  getTopicListHot() {
    var a = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.get_topic_list,
      data: {
        is_hot: 1,
        page: a.data.page,
        topic_name: a.data.searchData
      },
      success: function (t) {
        var list = a.data.topic_box.concat(t.data);
        0 == t.code && (a.setData({
          topic_box: list,
          user: t.user
        }));
        console.log(a.data.topic_box);
      },
      complete: function () {
        a.setData({
          page: ++a.data.page
        })
        getApp().core.hideLoading();
      }
    });
  },

  chosen(t) {
    var topic_id = t.currentTarget.dataset.topicId,
      s = this;
      console.log(topic_id);
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('someEvent', {
      topic_id: topic_id,
      goods_id: s.data.goods_id
    });
    if (s.data.back_url) {
      wx.navigateBack({
        delta: 1
      })
    }

  },
  onReachBottom: function () {
    this.getTopicListHot()
  },

  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url,
      s = this;
    if (!gopage) {
      return;
    }
    console.log(gopage, "去");
    wx.navigateTo({
      url: gopage,
      events: {
        someEvent: function (data) {
          console.log('返回了');
          s.loadData();
        }
      },
      success: function (res) {}
    })
  },
  searchInput(e) {
    this.setData({
      searchData: e.detail.value
    });
  },
  searchConfirm() {
    if (!this.data.searchData) {
      getApp().core.showToast({
        title: "搜索内容不能为空",
        icon: 'none'
      });
      return;
    }
    var that = this;
    this.setData({
      page: 1,
      topic_box:[]
    },function () {
      that.getTopicListHot()
    })
  },
})